package com.sdhs.paas.itims.app;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import com.alibaba.fastjson.TypeReference;
import com.sdhs.paas.sdk.base.common.BasePage;
import com.sdhs.paas.sdk.base.dto.PageInfoDTO;
import com.sdhs.paas.sdk.base.page.Page;
import com.sdhs.paas.sdk.base.utils.EntityDtoConvertUtil;
import com.sdhs.paas.sdk.core.PaasBaseRequest;
import com.sdhs.paas.sdk.core.PaasBaseResponse;
import com.sdhs.paas.sdk.core.RefreshableRestController;

import com.sdhs.paas.itims.bean.EquipmentGroupBean;
import com.sdhs.paas.itims.bean.ItimsGroupBean;
import com.sdhs.paas.itims.dto.EquipmentGroupDTO;
import com.sdhs.paas.itims.dto.ItimsGroupDTO;
import com.sdhs.paas.itims.dto.req.EquipmentGroupReqDTO;
import com.sdhs.paas.itims.dto.res.EquipmentGroupResDTO;
import com.sdhs.paas.itims.dto.res.ItimsGroupResDTO;
import com.sdhs.paas.itims.service.EquipmentGroupService;

/**
 * @author EquipmentGroup
 * @version 1.0
 * @Title:  代码生成接口，用于接受前端传输过来的数据
 * @author kangjichang
 * @date 2018-09-18
 */
 
@RefreshableRestController
@RequestMapping(value = "/equipmentGroup")
public class EquipmentGroupController {

    @Autowired
    private EquipmentGroupService equipmentGroupService;
    
    /**
     * 分页查询接口
     * */
    @PostMapping("/getEquipmentGroupPageList")
	public PaasBaseResponse<EquipmentGroupResDTO> getEquipmentGroupPageList(@RequestBody PaasBaseRequest<EquipmentGroupReqDTO> equipmentGroupReqDTO) {
		return null;
	}
	
	/**
	 * 保存接口
	 * */
	@PostMapping("/saveEquipmentGroup")
	public PaasBaseResponse<EquipmentGroupResDTO> saveEquipmentGroup(@RequestBody PaasBaseRequest<EquipmentGroupReqDTO> equipmentGroupReqDTO) {
		EquipmentGroupDTO equipmentGroupDTO = equipmentGroupReqDTO.getBody().getEquipmentGroupDTO();
		EquipmentGroupBean equipmentGroupBean = EntityDtoConvertUtil.convertToEntity(equipmentGroupDTO, EquipmentGroupBean.class);
		String uuid = UUID.randomUUID().toString().trim().replaceAll("-", "");
		equipmentGroupBean.setId(uuid);
		Boolean flag = equipmentGroupService.saveEquipmentGroup(equipmentGroupBean);
		equipmentGroupDTO.setId(uuid);
		EquipmentGroupResDTO equipmentGroupResDTO = new EquipmentGroupResDTO();
		equipmentGroupResDTO.setEquipmentGroupDTO(equipmentGroupDTO);
		return new PaasBaseResponse<>(equipmentGroupResDTO);
	}
	
	/**
	 * 保存接口
	 * */
	@PostMapping("/saveEquipmentGroupList")
	public PaasBaseResponse<EquipmentGroupResDTO> saveEquipmentGroupList(@RequestBody PaasBaseRequest<EquipmentGroupReqDTO> equipmentGroupReqDTO) {
		EquipmentGroupDTO equipmentGroupDTO = equipmentGroupReqDTO.getBody().getEquipmentGroupDTO();
		String groupId = equipmentGroupDTO.getGroupId();
		List<String> equipmentIdList = equipmentGroupDTO.getEquipmentIdList();
		EquipmentGroupBean equipmentGroup = new EquipmentGroupBean();
		List<EquipmentGroupBean> equipmentGroupBeanList = new ArrayList<EquipmentGroupBean>();
		equipmentGroup.setGroupId(groupId);
		List<EquipmentGroupBean>  equipmentList = equipmentGroupService.getList(equipmentGroup);
		for (String equipmentId : equipmentIdList) {
			//如果已经保存过了，就过滤掉
			if(equipmentList != null && equipmentList.size() > 0){
				if(equipmentList.contains(equipmentId)){
					continue;
				}
			}
			equipmentGroup = new EquipmentGroupBean();
			equipmentGroup.setGroupId(groupId);
			equipmentGroup.setEquipmentId(equipmentId);
			equipmentGroup.setId(UUID.randomUUID().toString().trim().replaceAll("-", ""));
			equipmentGroupBeanList.add(equipmentGroup);
		}
		if(equipmentGroupBeanList != null && equipmentGroupBeanList.size()>0){
			Boolean flag = equipmentGroupService.saveEquipmentGroupList(equipmentGroupBeanList);
		}
		EquipmentGroupResDTO equipmentGroupResDTO = new EquipmentGroupResDTO();
		return new PaasBaseResponse<>(equipmentGroupResDTO);
	}
	
	/**
	 * 修改接口
	 * */
	@PostMapping("/updateEquipmentGroup")
	public PaasBaseResponse<EquipmentGroupResDTO> updateEquipmentGroup(@RequestBody PaasBaseRequest<EquipmentGroupReqDTO> equipmentGroupReqDTO) {
		return null;
	}
	
	/**
	 * 获取详情接口
	 * */
	@PostMapping("/getEquipmentGroupDetail")
	public PaasBaseResponse<EquipmentGroupResDTO> getEquipmentGroupDetail(@RequestBody PaasBaseRequest<EquipmentGroupReqDTO> equipmentGroupReqDTO) {
		return null;
	}
	
	/**
	 * 删除接口
	 * */
	@PostMapping("/deleteEquipmentGroup")
	public PaasBaseResponse<EquipmentGroupResDTO> deleteEquipmentGroup(@RequestBody PaasBaseRequest<EquipmentGroupReqDTO> equipmentGroupReqDTO) {
		return null;
	}
}
